package com.medibooking.gwt.client.celendar.appointment;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.medibooking.gwt.client.calendar.CalendarState;
import com.medibooking.gwt.client.calendar.views.GridPosition;


@Singleton
public class AppointmentRenderer {

    private CalendarState calendarStateManager;
    private AppointmentWidget    renderingAppointment;
    private Element parentElement;
    private Element referenceElement;

    @Inject
    public AppointmentRenderer(CalendarState calendarStateManager) {
        this.calendarStateManager = calendarStateManager;
    }

    
    public void startRenderingAppointment(GridPosition gridPosition, Element referenceElement) {
        this.referenceElement = referenceElement;
        renderingAppointment = new AppointmentWidget(parentElement, referenceElement, gridPosition.getRow()*referenceElement.getClientHeight());
        GWT.log("Offset top "+referenceElement.getOffsetTop());
        
        
        GWT.log("Start Rendering at Column: " + gridPosition.getColumn() + " and row: " + gridPosition.getRow()+" TOP : "+ referenceElement.getAbsoluteTop());

    }

    public void updateEndHour(GridPosition gridPosition) {
        if (renderingAppointment != null) {
            GWT.log("Update Rendering at Column: " + gridPosition.getColumn() + " and row: " + gridPosition.getRow());
        }

    }

    public void stopRendering(GridPosition gridPosition) {
        if (renderingAppointment != null) {
            GWT.log("Stop Rendering at Column: " + gridPosition.getColumn() + " and row: " + gridPosition.getRow());
            renderingAppointment = null;
        }

    }

    private double getHourForGridPosition(GridPosition gridPosition) {
        return calendarStateManager.getTimeSlotSize().slotSize() * gridPosition.getRow();

    }

    /**
     * Given a column index, return the date
     * 
     * @param columnIndex
     * @return
     */
    private Date getDateForColumn(int columnIndex) {
        Date referenceDate = new Date(calendarStateManager.getStartDate().getTime());
        CalendarUtil.addDaysToDate(referenceDate, columnIndex - 1);

        return referenceDate;

    }

    public void setParentElement(Element parentElement) {
        this.parentElement = parentElement;
    }

    public Element getParentElement() {
        return parentElement;
    }

}
